printhdr "Configuring root"
chown root:root /
chmod 755 /
echo "${HOSTNAME}" > /etc/hostname

printhdr "Configuring locale"
echo "${LOCALE} UTF-8" > /etc/default/libc-locales
echo "LANG=${LOCALE}" > /etc/locale.conf

printhdr "Configure \`rc.conf\`"
cat << EOF > /etc/rc.conf 	
HOSTNAME="${HOSTNAME}"
HARDWARECLOCK="${HWCLOCK}"
TIMEZONE="${TIMEZONE}"
KEYMAP="${KEYMAP}"
EOF

printhdr "Configure fstab"
cp /proc/mounts /etc/fstab
BOOT_UUID=$(blkid -s UUID -o value /dev/disk/by-label/BOOT)
LUKS_UUID=$(blkid -s UUID -o value /dev/disk/by-label/ROOT) # Used by grub
ROOT_UUID=$(blkid -s UUID -o value /dev/mapper/cryptroot)
cat <<EOF > /etc/fstab
UUID=${BOOT_UUID} /boot/efi   vfat  defaults,noatime 0 2
UUID=${ROOT_UUID} /           btrfs ${FS_OPTS},subvol=@ 0 1
UUID=${ROOT_UUID} /home       btrfs ${FS_OPTS},subvol=@home 0 2
UUID=${ROOT_UUID} /.snapshots btrfs ${FS_OPTS},subvol=@snapshots 0 2
tmpfs /tmp tmpfs defaults,noatime,mode=1777 0 0
EOF

printhdr "Configuring dracut"
mkdir -p /boot/grub
dd bs=512 count=4 if=/dev/urandom of=/boot/volume.key
echo "Please enter password for: ${ROOT}"
cryptsetup luksAddKey "${ROOT}" /boot/volume.key
chmod 000 /boot/volume.key
chmod -R g-rwx,o-rwx /boot
echo "crypt ${ROOT} /boot/volume.key luks" > /etc/crypttab
echo "hostonly=yes" >> /etc/dracut.conf
echo 'install_items+=" /boot/volume.key /etc/crypttab "' > /etc/dracut.conf.d/10-crypt.conf
echo 'add_dracutmodules+=" crypt btrfs plymouth plymouth-encrypt resume "' >> /etc/dracut.conf
echo 'tmpdir=/tmp' >> /etc/dracut.conf
dracut --force --hostonly --no-kernel

printhdr "Installing grub"
echo "GRUB_ENABLE_CRYPTODISK=y" > /etc/default/grub
sed -i "/GRUB_CMDLINE_LINUX_DEFAULT=/s/\"$/ rd.auto=1 cryptdevice=UUID=$LUKS_UUID:allow-discards i915.modeset=1&/" /etc/default/grub
if [ "${BOOT_TYPE}" = "UEFI" ]; then
    grub-mkconfig -o /boot/grub/grub.cfg
    grub-install --target=x86_64-efi --efi-directory=/boot/efi --bootloader-id=willows --boot-directory=/boot --recheck
else
    grub-install "${ROOT}"
fi

# Add groups
for GROUP in $USER_GROUPS; do 
    groupadd "${GROUP}"
done

# Add new user and change passwords
printhdr "New Root Password?"
passwd
printhdr "Adding user: ${USERNAME}"
useradd -m -G "${USER_GROUPS}" -s /bin/zsh "${USERNAME}"
echo "Password For User ${USERNAME}"
passwd "${USERNAME}"

# Configure sudo/doas and limits
newperms "%wheel ALL=(ALL) ALL\n%wheel ALL=(ALL) NOPASSWD: /usr/bin/halt,/usr/bin/reboot,/usr/bin/wifi-menu,/usr/bin/mount,/usr/bin/umount,/usr/bin/sv,/usr/bin/xbps-install,/usr/bin/xbps-remove,/usr/bin/vpm,/usr/bin/loadkeys"
printf "@audio - rtprio 95\n@audio - memlock unlimited\n" > /etc/security/limits.conf
cat <<EOF >> /etc/doas.conf
permit ${USERNAME} as root
EOF

# Configure lightdm
sed -i "s/#greeter-session=example-gtk-gnome/greeter-session=lightdm-gtk3-greeter/g" /etc/lightdm/lightdm.conf

# Auto starts qemu vm support
virsh net-autostart default

# Default
ln -s /etc/sv/lightdm /etc/runit/runsvdir/default
ln -s /etc/sv/dbus /etc/runit/runsvdir/default
ln -s /etc/sv/dhcpcd /etc/runit/runsvdir/default
ln -s /etc/sv/rsyncd /etc/runit/runsvdir/default
ln -s /etc/sv/polkitd /etc/runit/runsvdir/default
ln -s /etc/sv/virtlogd /etc/runit/runsvdir/default
ln -s /etc/sv/libvirtd /etc/runit/runsvdir/default
ln -s /etc/sv/elogind /etc/runit/runsvdir/default
ln -s /etc/sv/pipewire /etc/runit/runsvdir/default
ln -s /etc/sv/pipewire-pulse /etc/runit/runsvdir/default
ln -s /etc/sv/bluetoothd /etc/runit/runsvdir/default
ln -s /etc/sv/bluez-alsa /etc/runit/runsvdir/default
ln -s /etc/sv/udevd /etc/runit/runsvdir/default
ln -s /etc/sv/dropbear /etc/runit/runsvdir/default
ln -s /etc/sv/wpa_supplicant /etc/runit/runsvdir/default
ln -s /etc/sv/NetworkManager /etc/runit/runsvdir/default
ln -s /etc/sv/acpid /etc/runit/runsvdir/default
ln -s /etc/sv/crond /etc/runit/runsvdir/default

# Remove some tty terminals
rm /etc/runit/runsvdir/default/agetty-tty4
rm /etc/runit/runsvdir/default/agetty-tty5
rm /etc/runit/runsvdir/default/agetty-tty6

cp -r /willows/etc/* /etc/

printhdr "Applying Theme"
doas "${USERNAME}" /etc/willows/themes/${THEME}-${DESKTOP}/apply
